<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\admin\controller;
use think\Db;
//讲师管理

/**
 * Class 讲师管理
 * @package app\admin\controller
 */
class Teacher extends Base
{
    public function index(){
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('teacher')
                ->where(['acid'=>1,'is_verify'=>1,'is_test'=>0,'type'=>0])
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v){
                $data[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($v['uid']!=''){
                    $userinfo=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                    $data[$k]['yongjin']=$userinfo['yongjin'];
                }else{
                    $data[$k]['yongjin']='0.000';
                }
                $data[$k]['img']= to_media_url($data[$k]['img']);
                if(session('agid')!='1' && session('username')!='admin'){
                    if($v['tphone']){
                        $data[$k]['tphone']=substr_replace($v['tphone'],'*****',3,5);
                    }
                }
            }
            $total=Db::name('teacher')->where(['acid'=>1,'is_verify'=>1,'type'=>0])->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function jigou(){
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('teacher')
                ->where(['acid'=>1,'is_verify'=>1,'is_test'=>0,'type'=>1])
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v){
                $jigouinfo=db('peixunban')->where(['acid'=>1,'id'=>$v['jgid']])->find();
                if(!empty($jigouinfo)){
                    $data[$k]['jgname']=$jigouinfo['name'];
                }
                if($v['uid']!=''){
                    $userinfo=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                    $data[$k]['yongjin']=$userinfo['yongjin'];
                }else{
                    $data[$k]['yongjin']='0.000';
                }
                $data[$k]['img']= to_media_url($data[$k]['img']);
                if(session('agid')!='1' && session('agid')!='7'){
                    if($v['tphone']){
                        $data[$k]['tphone']=substr_replace($v['tphone'],'*****',3,5);
                    }
                }
            }
            $total=Db::name('teacher')->where(['acid'=>1,'is_verify'=>1,'type'=>1])->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function dsh(){
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('teacher')
                ->where(['acid'=>1,'is_verify'=>0,'type'=>0])
                ->page($Nowpage, $limits)
                ->order('addtime desc')
                ->select();
            foreach ($data as $k=>$v){
                $data[$k]['img']= to_media_url($data[$k]['img']);
                if(session('agid')!='1' && session('username')!='admin'){
                    if($v['tphone']){
                        $data[$k]['tphone']=substr_replace($v['tphone'],'*****',3,5);
                    }
                }
                $data[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
            $total=Db::name('teacher')->where(['acid'=>1,'is_verify'=>0,'type'=>0])->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function jg_dsh(){
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('teacher')
                ->where(['acid'=>1,'is_verify'=>0,'type'=>1])
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v){
                $jigouinfo=db('peixunban')->where(['acid'=>1,'id'=>$v['jgid']])->find();
                if(!empty($jigouinfo)){
                    $data[$k]['jgname']=$jigouinfo['name'];
                }
                $data[$k]['img']= to_media_url($data[$k]['img']);
                if(session('agid')!='1' && session('agid')!='7'){
                    if($v['tphone']){
                        $data[$k]['tphone']=substr_replace($v['tphone'],'*****',3,5);
                    }
                }
            }
            $total=Db::name('teacher')->where(['acid'=>1,'is_verify'=>0,'type'=>1])->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function sh_teacher(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $res=Db::name('teacher')->where('id',$id)
                ->update(['is_verify'=>1,'is_test'=>0]);
            if($res){
                $this->result(0,'审核成功');
            }else{
                $this->result(1,'审核失败');
            }
        }
    }
    public function teacher_list_json(){
        $map=array(
            'acid'=>1,
        );
        if(session('agid')=='6'){
            $admininfo=db('admin')->where('id',session('uid'))->find();
            if(!empty($admininfo)){
                $map['id']=$admininfo['tid'];
            }
        }
        $teachers=Db::name('teacher')
            ->where(['acid'=>1,'is_verify'=>1,'is_test'=>0])
            ->select();
        foreach ($teachers as $k=>$v){
            $tlist[$k]['value']=$v['id'];
            $tlist[$k]['label']=$v['imgname'];
        }
        $this->result(0,'成功',$tlist,0);
    }
    public function add_teacher(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('teacher')
                ->where('id',$id)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            if(session('agid')!='1'){
                $this->result('1','暂无权限');
            }else {
                $data = input('post.');
                unset($data['file']);
                if ($data['id'] != '') {
                    $res = Db::name('teacher')->where('id', $data['id'])->update($data);
                    if ($res) {
                        if ($res == 0) {
                            $this->result('0', '无更新');
                        } else {
                            $this->result('0', '修改成功');
                        }
                    } else {
                        $this->result('1', '修改失败');
                    }
                } else {
                    $data['acid'] = 1;
                    $data['addtime'] = time();
                    $data['password'] = md5(substr($data['tphone'], -6) . $data['acid']);
                    $res = Db::name('teacher')->insert($data);
                    if ($res) {
                        $this->result('0', '添加成功');
                    } else {
                        $this->result('1', '添加失败');
                    }
                }
            }
        }
        return $this->fetch();
    }
    public function add_jigou_teacher(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('teacher')
                ->where('id',$id)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            if(session('agid')!='1'){
                $this->result('1','暂无权限');
            }else {
                $data = input('post.');
                unset($data['file']);
                if ($data['id'] != '') {
                    $res = Db::name('teacher')->where('id', $data['id'])->update($data);
                    if ($res) {
                        if ($res == 0) {
                            $this->result('0', '无更新');
                        } else {
                            $this->result('0', '修改成功');
                        }
                    } else {
                        $this->result('1', '修改失败');
                    }
                } else {
                    $data['acid'] = 1;
                    $data['type'] = 1;
                    $data['addtime'] = time();
                    $data['tpassword'] = md5(substr($data['tphone'], -6) . $data['acid']);
                    $res = Db::name('teacher')->insert($data);
                    if ($res) {
                        $this->result('0', '添加成功');
                    } else {
                        $this->result('1', '添加失败');
                    }
                }
            }
        }
        return $this->fetch();
    }
    public function teacher_del(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $res=Db::name('teacher')->where('id',$id)->delete();
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function status_switch(){
        $id = input('param.id');
        $state = input('param.state');
        $data=array(
            'state'=>$state
        );
        $res=Db::name('teacher')->where('id',$id)->update($data);
        if($res){
            $this->result(200,'操作成功');
        }else{
            $this->result(500,'操作失败');
        }
    }
}
